﻿@using TaskoMask.BuildingBlocks.Contracts.Dtos.Tasks;
    using TaskoMask.BuildingBlocks.Contracts.Api.Tasks
@inject TaskApiService TaskApiService

<EditForm Model="TaskModel" OnValidSubmit="HandleAddTask" class="form">
    <DataAnnotationsValidator />
    <input type="hidden" @bind="TaskModel.CardId" />
    <input type="hidden" @bind="TaskModel.BoardId" />

    <div class="row">
        <div class="col-lg-12">
            <div class="input-group">

                <InputText @bind-Value="TaskModel.Title" class="form-control" placeholder="add a task"></InputText>

                <span class="input-group-btn">
                    <button type="submit" class="btn btn-add-task">add</button>
                </span>
            </div>
        </div>
    </div>
</EditForm>


@code {

    #region Fields and Parameters


    [Parameter]
    public string CardId { get; set; }

    [Parameter]
    public string BoardId { get; set; }

    AddTaskDto TaskModel = new AddTaskDto();



    #endregion Fields and Parameters



    #region Protected Methods

    protected override Task OnInitializedAsync()
    {
        TaskModel.CardId = CardId;
        TaskModel.BoardId = BoardId;

        return base.OnInitializedAsync();
    }


    #endregion Protected Methods


    #region private Methods




    /// <summary>
    /// 
    /// </summary>
    private async Task HandleAddTask()
    {
        var result = await TaskApiService.AddAsync(TaskModel);
        if (result.IsSuccess)
            TaskModel.Title = "";

        CommandResultHandler.Init()
            .WithResult(result)
            .WithComponentMessageType(MessageType.Task_Added)
            .ShowErrorToast(ToastService)
            .PublishMessage(ComponentMessageService);
    }



    #endregion private Methods

}
